package StuDemo.LinkedList;

/**
 * @Author: Dijah
 * @Date: 2022/12/15 10:35
 * @Desc: 反转链表的前N个节点
 *
 * 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
 * reverse(head,3)
 * 1 <- 2 <- 3     4 -> 5 -> 6 -> null
 * |               ^
 * |______________|
 * 返回上面的3这个节点,1指向4这个节点
 */
public class code2_reverseN {

    ListNode successor = null  ;

   public ListNode reverseN(ListNode head, int n){

        if(n == 1){
            // 记录第 n + 1 个节点
            successor = head.next;
            return head;
        }
        ListNode last = reverseN(head.next,n-1);
        head.next.next = head;
        head.next = successor;
        return last;
    }
}
